﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using ElectrosLTD.Models;

namespace ElectrosLTD.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult Index()
        {
            List<Product> prodList = new ProductServ.ProductWCFClient().GetAllProducts();
            return View("Index", prodList);
        }


        public ActionResult Details(int id)
        {
            Product p = new ProductServ.ProductWCFClient().GetProductById(id);
            int counter = 0;
            ProductModel pm = new ProductModel();

            foreach (User_Ratings ur in new ProductServ.ProductWCFClient().GetProductRatings(id))
            {
                pm.Averagerating += ur.Rating.Rate;
                counter++;
            }

            pm.productID = p.ID;
            pm.productName = p.Name;
            pm.Price = (decimal)p.Price;
            pm.Stock = p.Stock;
            pm.Features = p.Features;
            pm.noOfRatings = counter;
            if (counter != 0)
            {
                pm.Averagerating = pm.Averagerating / counter;
            }

            return View("~/Views/Product/Details.cshtml", pm);
        }

        //--------------------------------Shopping Cart Methods-----------------
        [HttpPost]
        public void AddToCart(int productID)
        {
            /* int c = varb.IndexOf(',');
             int productID = Convert.ToInt32(varb.Substring(0, c));
             int quantity = Convert.ToInt32(varb.Substring(c++));*/
            ShoppingCart sc = new ShoppingCart();
            sc.ProductId_FK = productID;
            sc.Username = HttpContext.User.Identity.Name;
            sc.OrderDate = DateTime.Today;
            sc.StockPurchased = 1;
            new ProductServ.ProductWCFClient().AddToCart(sc);
        }

        public ActionResult ShoppingCart()
        {
            List<ShoppingCart> scList = new ProductServ.ProductWCFClient().GetShoppingCartByUsername(HttpContext.User.Identity.Name);
            
            List<ShoppingCartModel> scmList = new List<ShoppingCartModel>();

            foreach (ShoppingCart sc in scList)
            {
                ShoppingCartModel scm = new ShoppingCartModel();

                Product p = new ProductServ.ProductWCFClient().GetProductById(sc.ProductId_FK);

                scm.shoppingCartID = sc.ID;
                scm.productId = p.ID;
                scm.productName = p.Name;
                scm.Image = p.Image;
                scm.Stock = sc.StockPurchased;
                scm.Price = (decimal)p.Price;
                scmList.Add(scm);
            }
            return View("ShoppingCart",scmList);
        }

        public ActionResult DeleteFromCart(int shoppingCartID)
        {
            ShoppingCart sc = new ProductServ.ProductWCFClient().GetShoppingCartProduct(shoppingCartID);
            new ProductServ.ProductWCFClient().DeleteFromCart(sc);
            return ShoppingCart();
        }

        public ActionResult PurchaseItems()
        {
            Order o = new Order();
            o.ID = new Guid();
            o.PurchaseDate = DateTime.Now;
            o.Username = HttpContext.User.Identity.Name;

            new ProductServ.ProductWCFClient().AddToOrders(o);

            foreach (ShoppingCart sc in new ProductServ.ProductWCFClient().GetShoppingCartByUsername(HttpContext.User.Identity.Name))
            {
                OrderDetail od = new OrderDetail();
                od.OrderID_FK = o.ID;
                od.Quantity = sc.StockPurchased;
                od.ProductId_FK = sc.ProductId_FK;
                Product pr = new ProductServ.ProductWCFClient().GetProductById(sc.ProductId_FK);
                od.Price = pr.Price;
                od.Status_FK = 1;

                new ProductServ.ProductWCFClient().AddToOrderDetails(od);
                new ProductServ.ProductWCFClient().DeleteFromCart(sc);
            }
            //Send Invoice for the purchased Items
            return RedirectToAction("Index", "Home");
        }

        //--------------------------------Order Methods-----------------

        public ActionResult Order()
        {
            List<Order> orderList = new ProductServ.ProductWCFClient().GetOrder(HttpContext.User.Identity.Name);
            return View("Order", orderList);
        }

        //--------------------------------Ratings & Comments Methods-----------------

        public ActionResult RateComment(int productID)
        {   
            RateCommentModel rcm = new RateCommentModel();
            rcm.ProductID = productID;
            return View("RateComment", rcm);
        }

        [HttpPost]
        //the user must pass the product Ratings
        public ActionResult RateComment(RateCommentModel rcm)
        {
            {
                //--------Adds the ratings of the newly added ------------
                User_Ratings ur = new User_Ratings();
                ur.Username_Fk = HttpContext.User.Identity.Name;
                ur.Ratings_Fk =  rcm.Rating;
                ur.Product_Fk = rcm.ProductID;

                new ProductServ.ProductWCFClient().AddRating(ur);
                
                //--------Adds the comments of the newly added ------------
                Comment c = new Comment();
                c.Description = rcm.Comment;
                c.ProductID_FK = rcm.ProductID;
                c.Username_FK = HttpContext.User.Identity.Name;

                new ProductServ.ProductWCFClient().AddComment(c);

                return RedirectToAction("Order", "Product");
            }
        }

        public void CreatePruchasePDF()
        {
        }
    }
}
